<template>
    <view class="content">
        <view class="nav">
            <view class="nav-i" :class="{active : type == 1}" @click="chooseNav(1)">商品</view>
            <view class="nav-i" :class="{active : type == 2}" @click="chooseNav(2)">牛源信息</view>
        </view>
        
        <view v-if="type == 1">
            <view class="list_item" v-for="(item, index) in list" :key="index">
            	<image class="check-img" :src="item.checked == true ? '/static/icon/g_choose.png' : '/static/icon/g_choose2.png'" @click.stop="choose(item)"></image>
            	<view class="item" @click.stop="$gTo(`/pages/shop/goods?id=${item.data_id}`)">
                    <image :src="item.goods.image_uri" class="item_img" mode="aspectFit"></image>
                    <view class="right">
                        <view class="name">{{item.goods.goods_name}}</view>
                        <!-- <view class="sub">12公斤大容量</view> -->
                        <view class="price">￥<text class="price-num">{{item.goods.sale_price}}</text></view>
                    </view>
                </view>
            </view>
        </view>
        
        <view v-if="type == 2">
            <view class="item2" v-for="(item, index) in list" :key="index">
                <view class="u-flex u-row-between">
                    <image src="../../static/image/g_img_1.png" class="img-1" v-if="item.calf.member_type_txt == '个人'"></image>
                    <image src="../../static/image/g_img_2.png" class="img-1" v-else></image>
                    <image class="check-img u-m-r-20 u-m-t-10" :src="item.checked == true ? '/static/icon/g_choose.png' : '/static/icon/g_choose2.png'" @click="choose(item)"></image>
                </view>
                <view class="item-d" @click.stop="$gTo(`/pages/calf/calf-detail?id=${item.data_id}`)">
                    <view class="top2">
                        <view class="title">{{item.calf.title}}</view>
                        <view class="sta">{{item.calf.type_txt}}</view>
                        <!-- <view class="sta sta2">求购</view> -->
                    </view>
                    <view class="text">{{item.calf.describe}}</view>
                    <view class="img-box" v-if="item.calf.image_uri_txt.length > 0">
                        <view class="u-rela" v-for="(el, ind) in item.calf.image_uri_txt" :key="ind" v-if="ind < 3">
                            <image :src="el.attach_uri" class="img"></image>
                            <view class="img-num" v-if="item.calf.image_uri_txt.length > 3 && ind == 2">+{{ item.calf.image_uri_txt.length - 3 }}</view>
                        </view>
                    </view>
                    <view class="down">
                        <view @click.stop="$call(item.calf.telephone)">
                            <image src="../../static/icon/tel2.png" class="tel2"></image>
                            <view>电话</view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        
        <view style="height: 94rpx;padding-top: 30rpx;">
            <u-loadmore :status="status" v-if="status != 'loadmore'" />
        </view>
        
        <view class="footer">
            <view class="f-left" @click="allChoose">
                <image v-if="isChecked" src="/static/icon/g_choose.png" class="choose-02"></image>
                <image v-if="!isChecked" src="/static/icon/g_choose2.png" class="choose-02"></image>
                <view class="u-p-l-10">全选</view>
            </view>
            <view class="del" @click="delList">删除</view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                type: 1,
                status: 'loadmore', //是否显示没有更多了
                page: 1,
                list: [],
                isChecked: false,
            };
        },
        onLoad() {
            this.getList()
        },
        onReachBottom() {
        	if(this.page == 1){return}
        	this.getList()
        },
        methods: {
            chooseNav(num) {
                if (this.type == num) return
                this.isChecked = false
                this.type = num
                this.page = 1
                this.status = 'loadmore'
                this.list = []
                this.getList()
            },
            
            // 选中每一个项目
            choose(item) {
                var sellectList = []
                item.checked = !item.checked;
                this.list.forEach((el, index) => {
                    if (el.checked) {
                        sellectList.push(el)
                    }
                })
                if (sellectList.length == this.list.length) {
                    this.isChecked = true
                } else {
                    this.isChecked = false
                }
            },
            		
            // 全选
            allChoose() {
                this.isChecked = !this.isChecked
                if (this.isChecked) {
                    this.list.forEach((item, index) => {
                        item.checked = true
                    })
                } else {
                    this.list.forEach((item, index) => {
                        item.checked = false
                    })
                }
            },
            		
            // 删除收藏列表
            delList() {
                var ids = [];
                this.list.forEach(el => {
                    if (el.checked) {
                        ids.push(el.id)
                    }
                })
                if (ids.length == 0) {
                    this.$toast('请选择项目');
                    return false;
                }
                this.$ajax('deleteCollect', {
                    userToken: this.$getSync('userToken'),
                    id: ids.join(','),
                }).then(ret => {
                    if (ret.status == 0) {
                        this.isChecked = false
                        
                        this.page = 1
                        this.status = 'loadmore'
                        this.list = []
                        this.getList()
                    } else {
                        this.$toast(ret.message);
                    }
                });
            },
            
            // 获取列表
            getList() {
                let tab = ''
                if (this.type == 1) {
                    tab = 'product'
                } else {
                    tab = 'calf'
                }
                
            	if (this.status == 'nomore') return;
            	this.status = 'loading';
            	this.$ajax('collectionList', {
            		userToken: this.$getSync('userToken'),
            		page: this.page,
            		limit: 10,
                    type: tab,//商品product 牛源calf
            	}).then(ret => {
            		if (ret.status == 0) {
            			if (ret.data && ret.data.length > 0) {
            				this.list = this.list.concat(ret.data);
            				this.page++;
            				this.status = 'loadmore'
                            
                            this.list.forEach(cur => {
                                this.$set(cur, 'checked', false)
                            })
            			} else {
            				this.status = 'nomore'
            			}
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
        }
    };
</script>

<style lang="scss">
    page{
        background-color: #f6f6f6;
    }
    .content{
        padding-bottom: 100px;
    }
    .nav{
        border-top: 2rpx solid #ebebeb;
        background-color: #fff;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20rpx;
    }
    .nav-i{
        width: calc(100vw / 2);
        padding: 30rpx 0;
        font-size: 28rpx;
        text-align: center;
    }
    .active{
        font-size: 32rpx;
        font-weight: bold;
        color: #00734c;
        border-bottom: 2rpx solid #00734c;
    }
    
    .list_item {
    	width: 100vw;
    	border-bottom: 2rpx solid #e6e6e6;
    	padding: 36rpx 20rpx;
        display: flex;
    	justify-content: space-between;
    	align-items: center;
    	background-color: #FFFFFF;
    	box-sizing: border-box;
    }
    .check-img {
    	width: 31upx;
    	height: 31upx;
    }
    
    .item_img {
    	width: 22vw;
    	height: 22vw;
    	border-radius: 10rpx;
    	margin-right: 15rpx;
    	border: 2rpx solid #e3e3e3;
        overflow: hidden;
    }
    .right{
        width: 63vw;
        height: 22vw;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    .name{
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .sub{
        font-size: 24rpx;
        color: #6d6d6d;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .item{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .right > view:first-child{
        font-size: 28rpx;
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .price{
        padding-top: 15rpx;
        color: #f62a2a;
        font-size: 24rpx;
    }
    .price-num{
        font-size: 34rpx;
        font-weight: bold;
    }
    
    .footer{
        width: 100vw;
        height: 100rpx;
        background-color: #fff;
        padding: 0 25rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 24rpx;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 999;
    }
    .f-left{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .del{
        font-size: 28rpx;
        color: #fff;
        padding: 18rpx 70rpx;
        background-color: #00734c;
        border-radius: 50rpx;
    }
    .choose-02{
        width: 33rpx;
        height: 33rpx;
    }
    
    
    .item2{
        background-color: #fff;
        border-radius: 15rpx;
        margin: 0 20rpx 20rpx;
    }
    .img-1{
        width: 90rpx;
        height: 42rpx;
    }
    .item-d{
        padding: 10rpx 30rpx 30rpx;
    }
    .top2{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .title{
        font-size: 34rpx;
        font-weight: bold;
        word-break: break-all;
        white-space: pre-line;
        padding-right: 15rpx;
    }
    .sta{
        padding: 4rpx 15rpx;
        border-radius: 6rpx;
        font-size: 23rpx;
        color: #ff5f2b;
        background-color: #fff1ec;
    }
    .sta2{
        color: #0075ff;
        background-color: #dfeeff;
    }
    .text{
        font-size: 28rpx;
        word-break: break-all;
        white-space: pre-line;
        padding-top: 15rpx;
    }
    .img-box{
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-top: 10rpx;
    }
    .img{
        width: 200rpx;
        height: 200rpx;
        border: 2rpx solid #dedede;
        border-radius: 10rpx;
        margin-right: 20rpx;
        box-sizing: border-box;
    }
    .img:nth-child(3n) {
        margin-right: 0;
    }
    .img-num{
        font-size: 26rpx;
        color: #fff;
        text-align: center;
        line-height: 200rpx;
        width: 200rpx;
        height: 200rpx;
        border-radius: 10rpx;
        background: rgba(0, 0, 0, .5);
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
    }
    .down{
        font-size: 26rpx;
        color: #595b62;
        padding-top: 20rpx;
        display: flex;
        justify-content: flex-end;
        align-items: center;
    }
    .down>view{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 50rpx;
    }
    .tel2{
        width: 27rpx;
        height: 27rpx;
        margin-right: 10rpx;
    }

</style>
